package com.wedo.mall.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wedo.mall.R;

import butterknife.BindColor;
import butterknife.BindDimen;
import butterknife.ButterKnife;

/**
 * Created by lxy on 2017/6/18.
 */

public class LabelInputView extends RelativeLayout {

    @BindDimen(R.dimen.e_margin_mid)    int mMargin;
    @BindDimen(R.dimen.e_font_16)       int mTextSize;
    @BindDimen(R.dimen.e_height_50)     int mWidgetHeight;
    @BindColor(R.color.e_white)         int mBackgroundColor;

    @BindDimen(R.dimen.e_width_100)     int mLabelWidth;
    @BindColor(R.color.e_text_color)    int mLabelTextColor;

    @BindColor(R.color.gray5)           int mDividerColor;
    @BindDimen(R.dimen.e_line)          int mDividerHeight;

    public TextView tv_label;
    public EditText et_value;

    public LabelInputView(Context context) {
        this(context,null);
    }

    public LabelInputView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LabelInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
        initAttrs(context,attrs);
    }

    private void init(Context context){

        ButterKnife.bind(this);
        tv_label = new TextView(context);
        et_value = new EditText(context);
        View divider = new View(context);

        /**TextView*/
        tv_label.setId(R.id.tv_label);
        tv_label.setTextColor(mLabelTextColor);
        tv_label.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);
        LayoutParams tParams = new LayoutParams(mLabelWidth,mWidgetHeight);
        tParams.leftMargin = mMargin;
        tv_label.setGravity(Gravity.CENTER_VERTICAL);
        addView(tv_label,tParams);

        tv_label.setText("公司名称");

        /**EditText*/
        et_value.setId(R.id.et_value);
        et_value.setBackground(null);
        et_value.setTextColor(mLabelTextColor);
        et_value.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);
        LayoutParams eParams = new LayoutParams(LayoutParams.MATCH_PARENT,mWidgetHeight);
        eParams.rightMargin = mMargin;
        eParams.addRule(RIGHT_OF,R.id.tv_label);
        addView(et_value,eParams);

        et_value.setText("value");

        /**View*/
        divider.setBackgroundColor(mDividerColor);
        LayoutParams vParams = new LayoutParams(LayoutParams.MATCH_PARENT,mDividerHeight);
        vParams.addRule(ALIGN_PARENT_BOTTOM);
        addView(divider,vParams);

        this.setBackgroundColor(mBackgroundColor);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        Resources res = context.getResources();
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LabelInputView);

        String label = array.getString(R.styleable.LabelInputView_label);
        tv_label.setText(label);

        String value = array.getString(R.styleable.LabelInputView_value);
        String hint = array.getString(R.styleable.LabelInputView_hint);
        int gravity = array.getInt(R.styleable.LabelInputView_value_gravity,0);
        et_value.setText(value);
        et_value.setHint(hint);
        if (gravity == 1){
            et_value.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
        }

        array.recycle();
    }

    public String text(){
        return et_value.getText().toString();
    }

    public boolean isEmpty(){
        return TextUtils.isEmpty(text());
    }

    public String label(){
        return tv_label.getText().toString().trim();
    }

    public LabelInputView label(String label){
        tv_label.setText(label);
        return this;
    }

    public LabelInputView value(String value){
        et_value.setText(value);
        return this;
    }

    public LabelInputView hint(String hint){
        et_value.setHint(hint);
        return this;
    }

    public LabelInputView label(int label){
        tv_label.setText(label);
        return this;
    }

    public LabelInputView value(int value){
        et_value.setText(value);
        return this;
    }

    public LabelInputView hint(int hint){
        et_value.setHint(hint);
        return this;
    }

    public LabelInputView right(){
        et_value.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
        return this;
    }

    public LabelInputView left(){
        et_value.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
        return this;
    }

    public LabelInputView singleLine(){
        et_value.setSingleLine();
        return this;
    }
}
